package com.hp.docker_base.service.impl;

import com.alibaba.fastjson.JSON;
import com.hp.docker_base.algorithm.*;
import com.hp.docker_base.algorithm.ActivedRulesDto;
import com.hp.docker_base.algorithm.DataInDto;
import com.hp.docker_base.algorithm.DataOutDto;
import com.hp.docker_base.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc 2、服务实现的接口
 * @Author bee
 * @Date 2020/10/9 10:50
 */
@Service
public class ThroatService {


    @Autowired
    private MDC2ServiceImpl imdc2Service;


    public ThroatFidOutDto getMedicalResult(
            String RecId,
            String DeptId,
            String DataIn) throws IOException {
        // 0、已经解析处理好的数据
        //System.out.println();
        ArrayList<ArrayList<Double>> belief_degree = imdc2Service.getBeliefValueList();
        ArrayList<ArrayList<Double>> rule_input = imdc2Service.getReferenceValueList();

        List<Reference> references = new ArrayList<>();
        for(int i=0; i<rule_input.size(); i++){
            Reference reference = new Reference();
            reference.setId01(rule_input.get(i).get(0).toString());
            reference.setId02(rule_input.get(i).get(1).toString());
            reference.setId03(rule_input.get(i).get(2).toString());
            reference.setId04(rule_input.get(i).get(3).toString());
            reference.setId05(rule_input.get(i).get(4).toString());
            reference.setId06(rule_input.get(i).get(5).toString());
            reference.setId07(rule_input.get(i).get(6).toString());
            reference.setId08(rule_input.get(i).get(7).toString());
            reference.setId09(rule_input.get(i).get(8).toString());
            reference.setId10(rule_input.get(i).get(9).toString());
            reference.setId11(rule_input.get(i).get(10).toString());
            reference.setId12(rule_input.get(i).get(11).toString());
            reference.setId13(rule_input.get(i).get(12).toString());
            reference.setId14(rule_input.get(i).get(13).toString());
            reference.setId15(rule_input.get(i).get(14).toString());
            reference.setId16(rule_input.get(i).get(15).toString());
            references.add(reference);
        }

        List<Belief> beliefs = new ArrayList<>();
        for(int i=0; i<belief_degree.size(); i++){
            Belief belief = new Belief();
            belief.setSk01(belief_degree.get(i).get(0).toString());
            belief.setSk02(belief_degree.get(i).get(1).toString());
            belief.setSk03(belief_degree.get(i).get(2).toString());
            beliefs.add(belief);
        }

        List<DataInDto> dataInDtos = JSON.parseArray(DataIn, DataInDto.class);

        ArrayList<ArrayList<Double>> input_data = new ArrayList<ArrayList<Double>>();

        ArrayList<Double> Temp = new ArrayList<Double>();
        for (int i = 0; i < 16; i++) {
            Temp.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        input_data.add(Temp);

        int continuous_num = 1;
        int discrete_num = 15;

        ArrayList<Double> inputStandard = new ArrayList<Double>();
        inputStandard.add(30.01);
        inputStandard.add(50.01);
        inputStandard.add(80.01);

        BRBInference2 BRB = new BRBInference2(belief_degree, input_data, rule_input, continuous_num, discrete_num, inputStandard);

        BRB.Data_Init();
        BRB.Parameter();
        BRB.fusion();

        List<DataOutDto> DataOutList = new ArrayList<DataOutDto>();
        for (int i = 0; i < BRB.m_xin.get(0).size(); i++) {
            DataOutDto dataOutDto = new DataOutDto();
            if(i<9){
                dataOutDto.setFidOut("sk0" + (i + 1));
            }else {
                dataOutDto.setFidOut("sk" + (i + 1));
            }
            dataOutDto.setValue(BRB.m_xin.get(0).get(i).toString());
            DataOutList.add(dataOutDto);
        }

        List<ActivedRulesDto> ActivedRuleList = new ArrayList<>();
        for (int i = 0; i < BRB.Activation_weight.get(0).size(); i++) {
            if (BRB.Activation_weight.get(0).get(i) > 0) {
                //System.out.println(BRB.Activation_weight.get(0).get(i));
                ActivedRulesDto activedRulesDto = new ActivedRulesDto();
                activedRulesDto.setRId(i + 1);
                activedRulesDto.setWeight(BRB.Activation_weight.get(0).get(i));
                Map<String, ArrayList<Double>> Value_Map = new HashMap<>();
                Value_Map.put("reference", rule_input.get(i));
                Value_Map.put("belief", belief_degree.get(i));

                //activedRulesDto.setRule(Value_Map);
                ActivedRuleList.add(activedRulesDto);
            }
        }

        ThroatFidOutDto retList = new ThroatFidOutDto();
        retList.setResult(DataOutList);
        retList.setActivedRules(ActivedRuleList);
        retList.setRecId(RecId);
        retList.setDeptId(DeptId);
        retList.setReferences(references);
        retList.setBeliefs(beliefs);

        return retList;
    }


    public List<String> updateService(String rule) {

        ArrayList<ArrayList<Double>> rule_input = imdc2Service.getReferenceValueList();
        ArrayList<ArrayList<Double>> belief_degree = imdc2Service.getBeliefValueList();

        List<ThroatRule> ThroatRules = JSON.parseArray(rule, ThroatRule.class);

        List<String> tips = new ArrayList<>();


        int cover = 0;
        for (int k = 0; k < ThroatRules.size(); k++) {
            boolean flag = true;
            ThroatRule myRule = ThroatRules.get(k);

            if (myRule.getReference().get(0) < 30.01) {
                myRule.getReference().set(0, 1.0);
            } else if (myRule.getReference().get(0) > 30.01 && myRule.getReference().get(0) < 50.01) {
                myRule.getReference().set(0, 2.0);
            } else {
                myRule.getReference().set(0, 3.0);
            }
            for (int i = 0; i < rule_input.size(); i++) {
                int num = 0;
                //System.out.println();
                for (int j = 0; j < rule_input.get(i).size(); j++) {
                    if (myRule.getReference().get(j).toString().equals(rule_input.get(i).get(j).toString())) {
                        num++;
                    }
                }

                if (num == rule_input.get(i).size()) {

                    Belief belief = new Belief();
                    belief.setSk01(myRule.getBelief().get(0).toString());
                    belief.setSk02(myRule.getBelief().get(1).toString());
                    belief.setSk03(myRule.getBelief().get(2).toString());

                    String temp = JSON.toJSONString(belief);

                    MDC2 mdc2 = new MDC2();
                    mdc2.setRule(i+1);
                    mdc2.setBelief(temp);

                    imdc2Service.coverRule(mdc2);

                    tips.add("已覆盖第" + (i + 1) + "条规则");
                    cover++;
                    flag = false;
                    break;
                }

            }

            if (flag) {
                Reference reference = new Reference();
                Belief belief = new Belief();
                belief.setSk01(myRule.getBelief().get(0).toString());
                belief.setSk02(myRule.getBelief().get(1).toString());
                belief.setSk03(myRule.getBelief().get(2).toString());

                reference.setId01(myRule.getReference().get(0).toString());
                reference.setId02(myRule.getReference().get(1).toString());
                reference.setId03(myRule.getReference().get(2).toString());
                reference.setId04(myRule.getReference().get(3).toString());
                reference.setId05(myRule.getReference().get(4).toString());
                reference.setId06(myRule.getReference().get(5).toString());
                reference.setId07(myRule.getReference().get(6).toString());
                reference.setId08(myRule.getReference().get(7).toString());
                reference.setId09(myRule.getReference().get(8).toString());
                reference.setId10(myRule.getReference().get(9).toString());
                reference.setId11(myRule.getReference().get(10).toString());
                reference.setId12(myRule.getReference().get(11).toString());
                reference.setId13(myRule.getReference().get(12).toString());
                reference.setId14(myRule.getReference().get(13).toString());
                reference.setId15(myRule.getReference().get(14).toString());
                reference.setId16(myRule.getReference().get(15).toString());

                String temp0 = JSON.toJSONString(reference);
                String temp = JSON.toJSONString(belief);


                MDC2 mdc2 = new MDC2();
                mdc2.setRule(rule_input.size() + k + 1 - cover);
                mdc2.setReference(temp0);
                mdc2.setBelief(temp);

                imdc2Service.insertRule(mdc2);

                tips.add("已追加第" + (rule_input.size() + k + 1 - cover) + "条规则");
                flag = false;
            }


        }


        return tips;

    }


    public List<Map<String, Object>> showRules() {

        ArrayList<ArrayList<Double>> belief_degree = imdc2Service.getBeliefValueList();
        ArrayList<ArrayList<Double>> rule_input = imdc2Service.getReferenceValueList();

        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < rule_input.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("Rid", i + 1);
            map.put("reference", rule_input.get(i));
            map.put("belief", belief_degree.get(i));
            list.add(map);
        }

        return list;
    }


    public void deleteRule(int id) {
        imdc2Service.deleteRule(id);
    }
}
